<template>
    <div style="width: 1000px;">
        <div style="display: flex;">
            <div>
        		<el-button  @click="callPayPut"
        			:loading="loading_btn"
        			size="mini" type="primary">保存</el-button>
        	</div>
        </div>
		<br>
        <div class="searchForm">
            <div>
                制单日期： 
            	<el-date-picker
            	      size="small"
            	      v-model="buyer_date"
            		  value-format="yyyy-MM-dd"
            	      type="date"
            	      placeholder="选择日期时间">
            	    </el-date-picker>
            </div>
            <div>
                制单人： 
            	<el-input size="small" v-model="buyer_person" 
            	placeholder="请输入制单人"  
            	style="width:150px;"></el-input>
            </div>
			<div>
						  总金额：
			    <el-input size="mini" v-model="SumMoney" 
						  disabled
			    style="width:150px;"></el-input>
			</div>
        </div>
		<div class="flex">
			<div style="margin: 8px 10px;">
			    账户： <el-select size="small" v-model="bank_account" placeholder="请选择">
						<el-option
						  v-for="item in backList"
						  :key="item.id"
						  :label="item.account_title"
						  :value="item.account_title">
						</el-option>
					  </el-select>
			</div>
			<div style="margin-right: 20px;">
				<el-input type="textarea" 
					v-model="supplierList"
					placeholder="供应商列表"></el-input>
			</div>
			<div style="margin-right: 20px;">
			    <el-button type="primary" size="mini" @click="search">检索</el-button>
			</div>
			
		</div>
       
		 <br>
        <el-table  :data="list"  
			v-loading="loading"
			 border  
			 ref="multipleTable"
			 @selection-change="handleSelectionChange"
			>
			<el-table-column
			      type="selection"
			      width="55" />
			<el-table-column   align="center" width="150"  label="序号">
				<template slot-scope="scope">
				   <div>
						{{scope.$index+1}}
				   </div>
				</template>
			</el-table-column>
			<el-table-column   align="center" width="150"  :label="ttype==1?'供应商':'客户'">
				<template slot-scope="scope">
				   <div>
						<span v-if="ttype==1">{{scope.row.supplier}}</span>
						<span v-else>{{scope.row.customer}}</span>
				   </div>
				</template>
			</el-table-column>
			<el-table-column   align="center" width="200"  label="收款账户">
				<template slot-scope="scope">
					<div>
						{{scope.row.account_number}}
					</div>
				</template>
			</el-table-column>
			<el-table-column   align="center"   :label="ttype==1?'未付金额':'未收金额'">
				<template slot-scope="scope">
				   <div v-if="scope.row.code">
						 {{(scope.row.original_amount*1).toFixed(2) | toggleInt}}
				   </div>
				   <div v-else>
					    {{(scope.row.original_amount*1).toFixed(2) | toggleInt}}
				   </div>
				</template>
			</el-table-column>
			
			
        </el-table>
		<div style="display: flex;margin-top: 10px;">
		  <div>
			  已选合计：
		      <el-input size="mini" v-model="TotalMoney" 
			  disabled
		      style="width:150px;"></el-input>
		  </div>
		</div>
		
		
    </div>
</template>
<script>
    export default {
        data(){
            return {
                search_form:{
					buyer_person:"",
					supplier:"",
					bank_account:"",
					start_time:'',
					end_time:'',
					customer:'',
					supplierList:[]
                },
				time:[],
				backList:[],
			    buyer_date:'',
				checked:true,
				ttype:1,
				loading:false,
				supplierList:'',
				result:[],
				selectRes:[],
				bank_account:'',
				buyer_person:'',
				loading_btn:false,
            }
        },
		computed:{
			SumMoney(){
				let moneyTotal = 0
				for(let item of this.list){
					moneyTotal+=item.original_amount*1
				}
				return moneyTotal.toFixed(2)
			},
			TotalMoney(){
				let moneyTotal = 0
				for(let item of this.selectRes){
					moneyTotal+=item.original_amount*1
				}
				return moneyTotal.toFixed(2)
			},
		},
        mounted(){
			this.buyer_person = sessionStorage.getItem('user_name')
			let now = this.$utils.formatNowDate(2)
			this.time = [now,now]
			document.title = '批量付款'
			this.buyer_date = now
			this.lodeBackList()
        },
        methods:{
			callPayPut(){
				let res = this.selectRes.map(item=>{
					return item.supplier
				})
				if(res.length==0) return this.$message.error('请选择结算项目')
				let newArr = []
				for(let item of this.result){
					let index = res.findIndex(ele=>{
						return ele == item.customer
					})
					if(index>-1){
						newArr.push(item)
					}
				}
				
				if(!this.bank_account) return this.$message.error('请选择账户')
				for(let item of newArr){
					let order_number = this.$utils.generateRandomString(2)
					item.list = item.list.filter((val)=>{
						return val.id != '小计'
					})
					item.cid = sessionStorage.getItem('cid')
					for(let ele of item.list){
						ele.bank_account = this.bank_account
						
						ele.is_settle = 1,
						ele.is_settle_put = 1,
						ele.ttype = this.ttype
						ele.hander_person = sessionStorage.getItem('user_name')
						ele.buyer_date = this.buyer_date
						ele.prepared_by = sessionStorage.getItem('user_name')
						ele.order_number = order_number
						
					}
					
				}
				this.loading_btn = true
				this.$post('/putpay/batchPay',newArr,res=>{
						this.loading_btn = false
						if(res.data.code==200){
							this.$message.success('操作成功')
							this.clearSearch()
						}
						
				})
				
			},
			lodeBackList(){
				this.$post('/baseInfo/getAccountList',{status:1},res=>{
					this.backList = res.data.data
				})
			},
		    handleSelectionChange(row){
				this.selectRes = row
			},
			insertDD(){
				
				var oparray = [];
				var res = this.supplierList
				res = res.replace(/^\n*/, "");
				res = res.replace(/\n{2,}/g, "\n");
				res = res.replace(/\n*$/, "");
				oparray = res.split("\n");
				console.log(oparray)
				if(oparray.length){
					return oparray
				}
			},
			lodeResult(params){
				this.$post('/putpay/getPayPutDataNew',params,res=>{
					
					let listData = res.data.data 
					for(let item1 of listData){
						let newArr = []
						for(let el of item1.list){
							
							if(!el.bank_account){
								el.bank_account = ''
							}
							
							el.remarks = ''
							if(el.code_arr || el.code){
								if(el.code){
									if(this.ttype==2){
										el.price = -el.sales_price
										el.customer = el.supplier
									}else{
										el.price = -el.price
									}
								}else{
									if(this.ttype==2){
										el.price = el.sales_price
										el.customer = el.supplier
									}else{
										el.price = el.price
									}
								}
								// el.residue_money = el.residue_money?el.residue_money*el.goods_number:el.price*el.goods_number
								// el.residue_money_put = el.residue_money_put?el.residue_money_put*el.goods_number:el.price*el.goods_number
								el.residue_money = el.price*el.goods_number
								el.residue_money_put = el.price*el.goods_number
								
							}else{
								el.goods_number = 1
								el.price = -el.price
								el.residue_money = el.price*el.goods_number
								if(el.residue_money>0) el.residue_money = -el.residue_money
								el.one_pay = el.price*el.goods_number
							}
							if(this.ttype==1){
								el.one_pay = el.price*el.goods_number
							}else{
								if(el.code){
									el.one_pay = el.price*el.goods_number
								}else{
									el.one_pay = el.price*el.goods_number
								}
								
							}
							
							el.original_amount = el.price*el.goods_number
							newArr.push(el);
						}
						// newArr =  newArr.sort(this.compare1)
						item1.list = newArr
					}
					
					this.result = listData
				})
			},
			lodeList(){
				let supplierList = this.insertDD()
				if(supplierList.length==0){
					return this.$message.error('供应商不能为空')
				}
				let params = {
					supplierList:supplierList,
					type:this.ttype
				}
				this.lodeResult(params)
				
				this.$post('/putpay/getPayPutDataNew',params,res=>{
					let listData = res.data.data
					// this.result = res.data.data
						this.loading = false
						
						for(let item1 of listData){
							let newArr = []
							for(let el of item1.list){
								
								if(el.code || el.code_arr){
									
									if(el.code){
										if(this.ttype==2){
											el.price = -el.sales_price
										}else{
											el.price = -el.price
										}
									}else{
										if(this.ttype==2){
											el.price = el.sales_price
										}else{
											el.price = el.price
										}
									}
									
									el.residue_money = el.residue_money?el.residue_money*el.goods_number:el.price*el.goods_number
									el.residue_money_put = el.residue_money_put?el.residue_money_put*el.goods_number:el.price*el.goods_number
								}else{
									el.goods_number = 1
									el.price = -el.price
									el.residue_money = el.residue_money?el.residue_money*el.goods_number:el.price*el.goods_number
								}
								if(this.ttype==1){
									el.one_pay = el.residue_money*el.goods_number
								}else{
									if(el.code || el.code_arr){
										el.one_pay = el.residue_money_put*el.goods_number
									}else{
										el.one_pay = el.residue_money*el.goods_number
									}
									
								}
							
								el.original_amount = el.price*el.goods_number
								newArr.push(el);
							}
							// newArr =  newArr.sort(this.compare1)
							item1.list = newArr
						}
						
						
						
						let result = []
						
						//  scope.row.residue_money?scope.row.residue_money:scope.row.price
						for(let item of listData){
							let goods_number = 0
							let residue_money = 0
							let original_amount = 0
							let len = item.list.length
							if(item.list.length){
								item.list[0].sumRowSpan = len+1
							}
							let supplier
							let account_number
							let bank
							let bank_address
							let cnaps
							let type
							for(let ele of item.list){
								supplier = ele.supplier
								if(ele.back_info){
									account_number = ele.back_info.account_number
									bank = ele.back_info.bank
									bank_address = ele.back_info.bank_address
									cnaps = ele.back_info.cnaps
									type = ele.back_info.type
								}
								goods_number = goods_number+ele.goods_number*1
								original_amount = original_amount + ele.original_amount*1
								if(this.ttype==1){
									residue_money+=ele.original_amount*1
								}else{
									// let money_put = ele.code?ele.residue_money_put:ele.residue_money
									
									residue_money+=el.original_amount*1
								}
								
								
							}
							
							item.list.push(
							 {
								 id:'小计',
								 goods_name:len,
								 goods_number:goods_number,
								 residue_money:residue_money,
								 original_amount:original_amount,
								 supplier:supplier,
								 account_number:account_number,
								 bank:bank,
								 bank_address:bank_address,
								 cnaps:cnaps,
								 type:type
							 }
							)
							for(let ele of item.list){
								result.push(ele)
							}
							
							
						}
						this.list = result.filter(item=>{
							return item.id == '小计'
						})
						
					
				})
			},
			clearSearch(){
				this.supplierList = ''
				this.bank_account = ''
				this.list = []
				this.result = []
			},
           
            
        }
    }
</script>
<style>
	.el-table .warning-row {
	    background: oldlace;
	  }
	
	  .el-table .success-row {
	    background: #f0f9eb;
	  }
</style>
